﻿using AutoMapper;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Windows;
using System.Windows.Input;
using WpfApp.SaveInfos;
using MessageBox = HandyControl.Controls.MessageBox;

namespace WpfApp.ViewModels
{
    class MesConfigViewModel
    {
        public string? Url { get; set; }

        public string? UserName { get; set; }

        public string? Password { get; set; }

        public string? Site { get; set; }

        public string? Resource { get; set; }

        public string? ModeProcessSfc { get; set; }

        public string? Operation { get; set; }

        public string? Activity { get; set; }

        public string? OperationRevision { get; set; }

        public string? User { get; set; }

        public string? ProcessLot { get; set; }

        public ICommand SaveCommand { get; set; }

        public MesConfigViewModel(IMapper mapper, ILogger<MesConfigViewModel> logger)
        {

            var saveInfo = SaveInfo.Read<MesConfigSaveInfo>(MesConfigSaveInfo.MesConfigFile) ?? new MesConfigSaveInfo();
            mapper.Map(saveInfo, this);

            SaveCommand = new RelayCommand(() =>
            {
                try
                {
                    var saveInfo = mapper.Map<MesConfigViewModel, MesConfigSaveInfo>(this);
                    SaveInfo.Write(saveInfo, MesConfigSaveInfo.MesConfigFile);
                    MessageBox.Show("保存成功！", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("保存失败！", "错误提示", MessageBoxButton.OK, MessageBoxImage.Error);
                    logger.LogError(ex, "保存配置失败");
                }
            });
        }
    }
}
